#include <gtk/gtk.h>
#include <stdio.h>

#include "gtkutils.h"

#include "stringutils.h"

#define BUNDLE_CONFIG_TEMPLATE "                \n\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>      \n\
<plist version=\"1.0\">                         \n\
<dict>                                          \n\
%s                                              \n\
</dict>                                         \n\
</plist>                                        \n\
"

void sayHello() {
    printf("hello word\n");
}

struct ConfigFile {
    char key[100];
    char type[10];
    char required[10];
    char description[1000];
} PLists[] = {
    {.key = "CFBundleDevelopmentRegion",       .type = "String",    .required = "No",   .description = "该束的地区。通常对应于作者的母语"},
    {.key = "CFBundleDisplayName",             .type = "String",    .required = "No",   .description = "本地化的束名"},
    {.key = "CFBundleDocumentTypes",           .type = "Array",     .required = "No",   .description = "一组描述了该束所支持的文档类型的字典"},
    {.key = "CFBundleExecutable",              .type = "String",    .required = "Yes",  .description = "该束的可执行文件名"},
    {.key = "CFBundleGetInfoHTML",             .type = "String",    .required = "No",   .description = "用来在Finder的Get Info 面板中显示的更丰富内容的字符串"},
    {.key = "CFBundleGetInfoString",           .type = "String",    .required = "No",   .description = "用来在Finder的Get Info 面板中显示的字符串"},
    {.key = "CFBundleHelpBookFolder",          .type = "String",    .required = "No",   .description = "含有该束帮助文件的文件夹名字"},
    {.key = "CFBundleHelpBookName",            .type = "String",    .required = "No",   .description = "当该束的帮助启动时显示的帮助文件的名字"},
    {.key = "CFBundleIconFile",                .type = "String",    .required = "Yes",  .description = "图标文件的文件名"},
    {.key = "CFBundleIdentifier",              .type = "String",    .required = "Yes",  .description = "该束的唯一标识字符串。该字符串的格式类似java包的命名方式，例如：com.apple.myapp"},
    {.key = "CFBundleInfoDictionaryVersion",   .type = "String",    .required = "Yes",  .description = "Info.plist格式的版本信息"},
    {.key = "CFBundleName",                    .type = "String",    .required = "Yes",  .description = "束的简称"},
    {.key = "CFBundlePackageType",             .type = "String",    .required = "Yes",  .description = "用来标识束类型的四个字母长的代码"},
    {.key = "CFBundleShortVersionString",      .type = "String",    .required = "Yes",  .description = "面向用户市场的束的版本字符串"},
    {.key = "CFBundleSignature",               .type = "String",    .required = "Yes",  .description = "用来标识创建者的四个字母长的代码"},
    {.key = "CFBundleURLTypes",                .type = "Array",     .required = "No",   .description = "一组描述了该束所支持的URL协议的字典"},
    {.key = "CFBundleVersion",                 .type = "String",    .required = "Yes",  .description = "可执行文件的创建号"},
};

// CFBundleExecutable              String    Yes     该束的可执行文件名
// CFBundleIconFile                String    Yes     图标文件的文件名
// CFBundleIdentifier              String    Yes     该束的唯一标识字符串。该字符串的格式类似java包的命名方式，例如：com.apple.myapp
// CFBundleInfoDictionaryVersion   String    Yes     Info.plist格式的版本信息
// CFBundleName                    String    Yes     束的简称
// CFBundlePackageType             String    Yes     用来标识束类型的四个字母长的代码
// CFBundleShortVersionString      String    Yes     面向用户市场的束的版本字符串
// CFBundleSignature               String    Yes     用来标识创建者的四个字母长的代码
// CFBundleVersion                 String    Yes     可执行文件的创建号


GtkEntry *version_entry;
GtkEntry *identifire_entry;
GtkEntry *appname_entry;
GtkEntry *appexec_entry;
GtkEntry *appicon_entry;

bool config_parser_string(const char *key, const char *value, GSList **list) {
    char buffer[100] = {0};
    char string[100] = {0};
    char property[200] = {0};
    if (!isNull(value) && !LineIsEmpty(value)) {
        sprintf(buffer, "    <key>%s</key>", key);
        sprintf(string, "    <string>%s</string>", value);
        sprintf(property, "%s\n%s", buffer, string);

        *list = g_slist_append(*list, createBufferCopy(property));
    }
}

createSignalObjectFunc(clicked_action_preview_config, GtkTextBuffer) {
    GSList *list = NULL;

    config_parser_string("CFBundleName",       entryGetText(appname_entry),    &list);
    config_parser_string("CFBundleVersion",    entryGetText(version_entry),    &list);
    config_parser_string("CFBundleIconFile",   entryGetText(appicon_entry),    &list);
    config_parser_string("CFBundleExecutable", entryGetText(appexec_entry),    &list);
    config_parser_string("CFBundleIdentifier", entryGetText(identifire_entry), &list);


    char buffer[1000] = {0};;
    char config[10000] = {0};
    if (splitToStringWithSeparator(list, "\n", buffer)) {
        sprintf(config, BUNDLE_CONFIG_TEMPLATE, buffer);
        textBufferSetText(user_data, config, -1);
    }

    GS_FOREACH_FREE(list);
}

createSignalObjectFunc(clicked_action_generater_dialog, GtkWindow) {
    // GtkDialog *dialog = createDialog();
    // GtkDialog *dialog = createDialogWithButtons("Message", user_data, GTK_DIALOG_USE_HEADER_BAR, 
    //     "YES", GTK_RESPONSE_ACCEPT,
    //     "NO", GTK_RESPONSE_CANCEL);

    // GtkBox *box = dialogGetContentArea(dialog);
    // boxAppends(box, 
    //     createBoxWithVerticalAppends(5, 
    //         createBoxWithHorizntalMarginAlignAppends(5, 0, ACENTER, 
    //             createLabel("message"),
    //             createLabel("message"),
    //             createLabel("message"),
    //             createLabel("message"),
    //             createLabel("message"),
    //             NULL
    //         ),
    //         NULL
    //     ),
    //     NULL
    // );
    // setWidgetHorizntalExpand(box, true);

    // gtk_widget_set_size_request(dialog, 100, 50);
    // gtk_header_bar_new();

    
    // gtk_window_set_titlebar(user_data, bar);
    
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;

    window = createWindowWithTitleSize(app, "gtk-cmake-bundle-generator", 600, 300);

    // --------------------------------------------------
    appname_entry = createWidgetTooltipText(createEntry(), "束的简称");
    version_entry = createWidgetTooltipText(createEntryWithEntryBuffer(createEntryBufferWithText("0.0.1")), "可执行文件的创建号");
    appexec_entry = createWidgetTooltipText(createEntry(), "该束的可执行文件名");
    appicon_entry = createWidgetTooltipText(createEntry(), "图标文件的文件名");
    identifire_entry = createWidgetTooltipText(createEntry(), "该束的唯一标识字符串。该字符串的格式类似java包的命名方式，例如：com.apple.myapp");
    // --------------------------------------------------
    GtkTextBuffer *text_buffer = createTextBuffer(NULL);

    setWindowChild(window, 
        createBoxWithVerticalAppends(5, 
            createBoxWithHorizntalMarginAlignAppends(5, 0, ACENTER,
                createLabel("版本号:"),
                version_entry,
                createLabel("标识字符串:"),
                identifire_entry,
                NULL
            ),
            createBoxWithHorizntalMarginAlignAppends(5, 0, ACENTER,
                createLabel("应用名称:"),
                appname_entry,
                createLabel("应用图标:"),
                appicon_entry,
                NULL
            ),
            createBoxWithHorizntalMarginAlignAppends(5, 0, ACENTER,
                createLabel("应用可执行文件名称:"),
                appexec_entry,
                createButtonAction(text_buffer, "预览", clicked_action_preview_config),
                createButtonAction(window, "生成", clicked_action_generater_dialog),
                NULL
            ),
            createWidgetVerticalExpand(createTextViewWithTextBuffer(text_buffer), true),
            NULL
        )
    );
    showWindow(window);
}

int	main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = createApplicationConnectActivate("gtk.example.template", 
                                G_APPLICATION_FLAGS_NONE, activate);
    status = runApplication(app, argc, argv);
    objectUnref(app);

    return status;
}



// CFBundleDevelopmentRegion       String    No      该束的地区。通常对应于作者的母语
// CFBundleDisplayName             String    No      本地化的束名
// CFBundleDocumentTypes           Array     No      一组描述了该束所支持的文档类型的字典
// CFBundleExecutable              String    Yes     该束的可执行文件名
// CFBundleGetInfoHTML             String    No      用来在Finder的Get Info 面板中显示的更丰富内容的字符串
// CFBundleGetInfoString           String    No      用来在Finder的Get Info 面板中显示的字符串
// CFBundleHelpBookFolder          String    No      含有该束帮助文件的文件夹名字
// CFBundleHelpBookName            String    No      当该束的帮助启动时显示的帮助文件的名字
// CFBundleIconFile                String    Yes     图标文件的文件名
// CFBundleIdentifier              String    Yes     该束的唯一标识字符串。该字符串的格式类似java包的命名方式，例如：com.apple.myapp
// CFBundleInfoDictionaryVersion   String    Yes     Info.plist格式的版本信息
// CFBundleName                    String    Yes     束的简称
// CFBundlePackageType             String    Yes     用来标识束类型的四个字母长的代码
// CFBundleShortVersionString      String    Yes     面向用户市场的束的版本字符串
// CFBundleSignature               String    Yes     用来标识创建者的四个字母长的代码
// CFBundleURLTypes                Array     No      一组描述了该束所支持的URL协议的字典
// CFBundleVersion                 String    Yes     可执行文件的创建号
